package com.xiao.common.util;

import com.alibaba.fastjson.JSONObject;
import com.xiao.common.enums.ResultEnum;

import java.io.Serializable;

/**
 * @Author xiaoxiaoping
 * @Date 2019/10/29
 */
public class ResultUtil<T> implements Serializable {

    private static final long serialVersionUID = 3436477890959388499L;

    /**
     * 返回操作码（默认为正常）
     */
    private Integer code = ResultEnum.SUCCESS.code();

    /**
     * 返回数据信息
     */
    private T data;

    /**
     * 返回正确消息信息
     */
    private String msg = "";

    /**
     * 返回错误的消息信息
     */
    private String error = "";


    public ResultUtil(){

    }

    public ResultUtil(Integer code,String msg,T data,String error){
        this.code = code;
        this.msg = msg;
        this.error = error;
        this.data = data  == null ? (T) new JSONObject() : data;
    }

    public static <T> ResultUtil SUCCESS(String msg,T data){
        return new ResultUtil(ResultEnum.SUCCESS.code(),msg,data,null);
    }

    public static <T> ResultUtil FAIL(String error,T data){
        return new ResultUtil(ResultEnum.FAIL.code(),null,data,error);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
}
